export class ScalarType {

  name: string;
  description?: string;

  constructor(config: ScalarTypeConfig) {
    this.name = config.name;
    this.description = config.description;
    this.serialize = config.serialize;
    this.parseValue = config.parseValue;
  }
  
  serialize?(value: any): any;

  parseValue?(value: any): any;

  toString() {
    return this.name
  }
}


interface ScalarTypeConfig {
  name: string;
  description?: string;
  serialize?(value: any): any;
  parseValue?(value: any): any;
}


export const StringType = new ScalarType({

  name: 'string',

  serialize(value: any): string {
    if (typeof value === 'string') {
      return value
    }

    throw new Error(`String cannot represent value: ${value}`)
  },

  parseValue(value: any): string {
    return value
  },
});
